from PyQt5.QtWidgets import (QWidget, QDesktopWidget,QMessageBox,
                             QHBoxLayout, QVBoxLayout,QPushButton, QLabel)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QTimer
from PyQt5.QtMultimedia import QMediaPlayer
#导入自己建的类
from sound import Sound
from lyrics import LyricsWindow,Lyrics
from about import About
from file import File
from play import Play
from music_slider import MusicSlider
from mode import Mode
import images_rc

class MP3Player(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.initialization()
        self.file.loadingSetting()
        self.playBtn.clicked.connect(self.play.playMusic)

    def initUI(self):
        """UI设计"""
        self.player = QMediaPlayer()
        self.ms = MusicSlider(self)
        self.playBtn = QPushButton(self)
        # 歌词
        self.lyrics_window = LyricsWindow()
        self.lyrics = Lyrics(self)
        #音乐文件夹
        self.file = File(self)
        #播放及模式转换
        self.play = Play(self)
        self.mode = Mode(self)
        #声音
        self.sound = Sound(self)
        #底部文字
        self.textLable = QLabel('人生苦短，我用python🎈')
        self.about = About()
        #计时器
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.mode.playByMode)
        #方法
        self._Set_playBtn_Icons()
        self.Layoutsettings()

    def _Set_playBtn_Icons(self):
        """设置图标样式"""
        self.playBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/play.png)}")
        self.playBtn.setFixedSize(56, 56)

    def Layoutsettings(self):
        """布局"""
        self.hBoxSlider = QHBoxLayout()
        self.hBoxSlider.addWidget(self.ms.startTimeLabel)
        self.hBoxSlider.addWidget(self.ms.slider)
        self.hBoxSlider.addWidget(self.ms.endTimeLabel)
        self.hBoxButton = QHBoxLayout()
        self.hBoxButton.addWidget(self.mode.PlayModeBtn)
        self.hBoxButton.addStretch(2)
        self.hBoxButton.addWidget(self.sound.soundBtn)
        self.hBoxButton.addWidget(self.sound.soundslider)
        self.hBoxButton.addWidget(self.sound.soundvalueLabel)
        self.hBoxButton.addStretch(2)
        self.hBoxButton.addWidget(self.play.prevBtn)
        self.hBoxButton.addWidget(self.playBtn)
        self.hBoxButton.addWidget(self.play.nextBtn)
        self.hBoxButton.addStretch(4)
        self.hBoxButton.addWidget(self.lyrics.lyricsBtn)
        self.hBoxButton.addStretch(1)
        self.hBoxButton.addWidget(self.file.openBtn)

        self.vBoxControl = QVBoxLayout()
        self.vBoxControl.addLayout(self.hBoxSlider)
        self.vBoxControl.addLayout(self.hBoxButton)

        self.hBoxAbout = QHBoxLayout()
        self.hBoxAbout.addWidget(self.textLable)
        self.hBoxAbout.addStretch(1)
        self.hBoxAbout.addWidget(self.about.aboutBtn)

        self.vboxMain = QVBoxLayout()
        self.vboxMain.addWidget(self.file.musicList)
        self.vboxMain.addLayout(self.vBoxControl)
        self.vboxMain.addLayout(self.hBoxAbout)

        self.setLayout(self.vboxMain)

    def initialization(self):
        """初始化"""
        #界面初始化
        self.resize(600, 400)
        self._center()
        self.setWindowTitle('MPure3')
        self.setWindowIcon(QIcon(':/resource/images/music.png'))
        self.show()

    def _center(self):
        """窗口显示居中"""
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def closeEvent(self, event):
        """确认用户是否要真正退出"""
        reply = QMessageBox.question(self, 'Message',
            "确定要退出吗？", QMessageBox.Yes |
            QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()